<template>
  <div class="app-container">
    <div class="form-box">
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-tabs type="border-card" class="form-tabs">
          <el-tab-pane :label="$t('common.edit_info')" class="form-tabs-tab-pane">

            <!-- 类型 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.type')" prop="type">
                  <el-radio-group v-model.number="form.type" :placeholder="$t('links.type_tips')" :disabled="routerParams.id>0">
                    <el-radio v-for="item in dictionaryService.getItems('linksTypeVar')" :key="item.id" :label="Number(item.value)">{{ item.name }}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 友情链接名称 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.name')" prop="name">
                  <el-input
                    v-model="form.name"
                    :placeholder="$t('links.name_tips')"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- logo -->
            <el-row v-show="form.type==2" :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('links.logo_image')"
                  prop="logo_image"
                  :rules="{required: form.type===2, message: $t('links.logo_image_tips'), trigger: 'blur'}"
                >
                  <jsp-image-upload
                    :title="$t('links.logo_image')"
                    :default-list="logo_imageList"
                    :action="fileUploadConfig.imageAction"
                    :headers="fileUploadConfig.headers"
                    :size="Number(fileUploadConfig.allowImageSize)"
                    :accept="fileUploadConfig.allowImageTypeList"
                    :width="195"
                    :height="40"
                    @error="(error)=>{error!=null && $message.error(error)}"
                    @change="handleImageUploadChange"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 链接地址 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.href')" prop="href">
                  <el-input
                    v-model="form.href"
                    :placeholder="$t('common.href_tips')"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 打开方式 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('common.href_target')"
                  prop="href_target"
                >
                  <el-select
                    v-model="form.href_target"
                    :placeholder="$t('common.href_target_tips')"
                    clearable
                  >
                    <el-option
                      v-for="item in dictionaryService.getItems('hrefTargetTypeVar')"
                      :key="item.id"
                      :label="item.name"
                      :value="item.value"
                    />

                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 链接关系XFN -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.rel')" prop="rel">
                  <el-select
                    v-model="form.rel"
                    :placeholder="$t('common.rel_tips')"
                    clearable
                  >
                    <el-option
                      v-for="item in dictionaryService.getItems('relTypeVar')"
                      :key="item.id"
                      :label="item.name"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 客户名称 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('links.partner')" prop="partner">
                  <el-input
                    v-model="form.partner"
                    :placeholder="$t('links.partner_tips')"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 联系人 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('links.partner_linkman')"
                  prop="partner_linkman"
                >
                  <el-input
                    v-model="form.partner_linkman"
                    :placeholder="$t('links.partner_linkman_tips')"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 联系电话 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('links.partner_phone_number')"
                  prop="partner_phone_number"
                >
                  <el-input
                    v-model="form.partner_phone_number"
                    :placeholder="$t('links.partner_phone_number_tips')"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 发布状态 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('common.status')"
                  prop="status"
                >
                  <el-select
                    v-model.number="form.status"
                    :placeholder="$t('common.pleaseSelect')+$t('common.status')"
                    clearable
                  >
                    <el-option
                      v-for="item in dictionaryService.getItems('statusVar')"
                      :key="item.id"
                      :label="item.name"
                      :value="Number(item.value)"
                    />

                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>

        <el-row :gutter="20" type="flex" justify="center" class="btn-row">
          <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
            <el-form-item>
              <el-button
                type="primary"
                :loading="submitLoading"
                :disabled="routerParams.id>0&&!checkPermission(['admin.links.update']) || !routerParams.id&&!checkPermission(['admin.links.store'])"
                @click="onSubmit"
              >{{ $t('common.submit') }}</el-button>
              <el-button @click="onCancel">{{ $t('common.cancel') }}</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loadingStart } from '@/utils/loading-service'
import { show, update, store } from '@/api/links'
import JspImageUpload from '@/components/JspImageUpload'
import { mapGetters } from 'vuex'
import checkPermission from '@/utils/permission'

// export
export default {
  name: 'LinksEdit',
  components: {
    JspImageUpload
  },
  data() {
    return {
      submitLoading: false,
      form: {
        type: 1,
        name: '',
        logo_image: 0,
        href: '',
        href_target: '_blank',
        rel: '',
        partner: '',
        partner_linkman: '',
        partner_phone_number: '',
        status: 1
      },
      logo_imageList: [],
      // 表单输入校验 ，通过prop="name"属性进行控制
      rules: {
        name: [
          { required: true, message: this.$t('links.name_tips'), trigger: 'blur' },
          { min: 2, max: 50, message: this.$t('links.name_length_tips'), trigger: 'blur' }
        ]
      },
      // 路由参数
      routerParams: {},
      back_route_name: 'Links'
    }
  },
  computed: {
    ...mapGetters(['fileUploadConfig'])
  },
  created() {
    this.routerParams = this.$route.params
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    checkPermission,
    async getDetail() {
      const { id } = this.routerParams
      if (id > 0) {
        const loading = loadingStart()
        await show(id).then((response) => {
          for (const key in response.data) {
            if (key in this.form) {
              this.form[key] = response.data[key]
            }
          }
        }).catch(() => {
          this.closeBack()
        })
        // 封面图
        this.logo_imageList = this.form.logo_image
        this.form.logo_image = this.form.logo_image.map((item) => {
          return item.id
        }).join(',')
        loading.close()
      }
    },
    onSubmit() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          this.submitLoading = true
          let response
          if (this.routerParams.id) {
            response = await update(this.routerParams.id, this.form).catch(() => {})
          } else {
            response = await store(this.form).catch(() => {})
          }
          if (response instanceof Object) {
            this.$message.success(response.message)
            this.closeBack(this.back_route_name)
          }
          this.submitLoading = false
        } else {
          this.$message.error(this.$t('common.pleaseFillRequired'))
          return false
        }
      })
    },
    onCancel() {
      this.closeBack(this.back_route_name)
    },
    handleImageUploadChange(result) {
      this.form.logo_image = result.ids
    }
  }
}
</script>

<style scoped lang="scss">
</style>
